<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}"
            @reset="()=>{this.pagination.current=1;this.queryParams.pageNum=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="菜单名称" name="menuName">
            <t-input
              v-model="queryParams.menuName"
              clearable
              placeholder="请输入菜单名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="状态" name="status">
            <t-select style="width: 240px" placeholder="菜单状态" :options="dict.type.sys_normal_disable"
                      v-model="queryParams.status">
              <!--              <t-option v-for="(item, index) in statusOption" :value="item.value" :label="item.label" :key="index">-->
              <!--                {{ item.label }}-->
              <!--              </t-option>-->
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="dialog.header='添加菜单';dialog.menuId=0;dialog.parentId=0;dialog.open_add_edit_menu = true;"
                variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        新增
      </t-button>
      <t-button @click="onExpandAllToggle" variant="outline" theme="primary">
        <t-icon name="swap" slot="icon"/>
        展开/折叠
      </t-button>
    </div>
    <t-enhanced-table
      ref="table"
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :loading="dataLoading"
      :expandedTreeNodes.sync="expandedTreeNodes"
      :tree="{ childrenKey: 'children', defaultExpandAll: false, expandTreeNodeOnClick: true}"
    >
      <template #menuType="{ row }">
        <t-tag v-if="row.menuType==='M'" theme="primary" variant="light">目录</t-tag>
        <t-tag v-else-if="row.menuType==='C'" theme="success" variant="light">菜单</t-tag>
        <t-tag v-else-if="row.menuType==='CC'" theme="warning" variant="light">菜单子页面</t-tag>
        <t-tag v-else-if="row.menuType==='F'" theme="warning" variant="light">按钮</t-tag>
      </template>
      <template #status="{ row }">
        <t-tag v-if="row.status === '0'" theme="success" variant="light">正常</t-tag>
        <t-tag v-else theme="danger" variant="light">停用</t-tag>
      </template>
      <template #icon="{ row }">
        <t-icon :name="row.icon"></t-icon>
      </template>

      <template #op="{ row }">
        <t-button
          @click.stop="dialog.header='修改菜单';dialog.row=row;dialog.menuId=row.id;dialog.parentId=0;dialog.open_add_edit_menu = true;"
          size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon"/>
          修改
        </t-button>
        <t-button size="small"
                  @click.stop="dialog.header='添加菜单';dialog.menuId=0;dialog.parentId=row.id;dialog.open_add_edit_menu = true;"
                  variant="text" class="t-button-link">
          <t-icon name="add" slot="icon"/>
          新增
        </t-button>
        <t-button theme="danger" @click.stop="handleDelete(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon"/>
          删除
        </t-button>
      </template>
    </t-enhanced-table>
    <add-edit-menu @reload="getList" :header="dialog.header"
                   :serviceId="dialog.serviceId" :parentId="dialog.parentId" :menuId="dialog.menuId"
                   :visible.sync="dialog.open_add_edit_menu" v-if="dialog.open_add_edit_menu"></add-edit-menu>
  </div>
</template>

<script>
import {listMenu, getMenu, delMenu, addMenu, updateMenu} from "@/api/system/menu";
import addEditMenu from "./components/add-edit-menu.vue";

export default {
  name: "menu-index",
  dicts: ['sys_normal_disable'],
  components: {
    addEditMenu
  },
  data() {
    return {
      expandAll: true,
      expandedTreeNodes: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateRange: []
      },
      data: [],
      columns: [
        {
          colKey: "menuName",
          title: "菜单名称",
          width: 300
        },
        {
          colKey: "menuType",
          title: "菜单类型",
          align: "center"
        },
        {
          colKey: "icon",
          title: "图标",
          align: "center"
        },
        {
          colKey: "orderNum",
          title: "排序",
          align: "center"
        },
        {
          colKey: "perms",
          title: "权限标识",
          align: "center"
        },
        {
          colKey: "component",
          title: "组件路径",
          align: "center"
        },
        {
          colKey: "status",
          title: "状态",
          align: "center"
        },
        {
          colKey: "createTime",
          title: "创建时间",
          align: "center",
          width: 180
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 250
        },
      ],
      dataLoading: false,
      dialog: {
        serviceId: 0,
        parentId: 0,
        menuId: 0,
        open_add_edit_menu: false,
        header: "",
        row: null,
      }
    }
  },
  created() {
    const serviceId = this.$route.params && this.$route.params.serviceId;
    if (serviceId) {
      this.dialog.serviceId = serviceId;
      this.queryParams.serviceId = serviceId;
      this.getList();
    }
  },
  methods: {
    onSubmit() {
      this.getList();
    },
    /** 查询菜单列表 */
    getList() {
      this.dataLoading = true;
      listMenu(this.queryParams).then(response => {
        this.data = this.handleTree(response.data, "id");
        // this.$refs.table?.resetData(this.data);
        this.dataLoading = false;
        // this.$nextTick(()=> {
        //   this.onRowToggle(parentId); // 添加修改默认展开父节点
        // })
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    onExpandAllToggle() {
      this.expandAll = !this.expandAll;
      if (this.expandAll) {
        this.$refs.table.expandAll();
      } else {
        this.$refs.table.foldAll();
      }
    },
    // onRowEdit(row) {
    //   this.$refs.table.setData(row.id, {...row, children: this.dialog.row.children});
    // },
    // onRowAppendTo(parentId, row) {
    //   this.$refs.table.appendTo(parentId, row);
    // },
    // onRowRemove(row) {
    //   this.$refs.table.remove(row.id);
    // },
    // onRowToggle(parentId) {
    //   if(parentId!==undefined){
    //     if (parentId!==0){
    //       if(this.data.length>0) {
    //         const rowData = this.$refs.table.getData(this.data[0].id);
    //         this.$refs.table.toggleExpandData(rowData);
    //         if (this.data[0].id!==parentId){
    //           const rowData1 = this.$refs.table.getData(parentId);
    //           this.$refs.table.toggleExpandData(rowData1);
    //         }
    //       }else {
    //         const rowData1 = this.$refs.table.getData(parentId);
    //         this.$refs.table.toggleExpandData(rowData1);
    //       }
    //     }else if(this.data.length>0) {
    //       const rowData = this.$refs.table.getData(this.data[0].id);
    //       this.$refs.table.toggleExpandData(rowData);
    //     }
    //   }
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除名称为"${row.menuName}"的数据项？`,
        theme: "warning",
        onConfirm: ({e}) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          delMenu(row.id).then(
            (res) => {
              // this.onRowRemove(row);
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        },
      })
    }
  }
}
</script>

<style scoped>

</style>
